﻿#include "Pirhana.h"
//#include "Global.h"
#include "Resource.h"

Pirhana::Pirhana(LPDIRECT3DDEVICE9 d3ddv, LPDIRECT3DSURFACE9 BackBuffer, LPD3DXSPRITE SpriteHandler, float x, float y) : Object(x, y)
{ 
	_sprite = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(L"..\\Resources\\Images\\Pirhana_32.png"), 150, 0);
	//_sprite->SetFrame(0,0);

	 

	/*_x = _x + 32/2;
	_yOld = _y + 32;
	_y = _y + 3 ;*/

	_xTemp = _x + 32/2;
	_yOld = _y + 32;
	_yTemp = _y + 3 ;

	timeStop = 0;
	_vy = 0.05;
	object_State = IS_IDLE;
}

Pirhana::~Pirhana()
{
	/*if (_sprite != NULL)
	{
		delete _sprite;
	}*/
}

 
 

void Pirhana::Render()
{
	 if (object_State == IS_ACTIVING)
	 {
		 //_sprite->Render(_x, _y);
		 _sprite->Render(_xTemp, _yTemp);
		 
	 }
	
}

OBJECT_TYPE Pirhana::GetTypeObject()
{
	return OBJECT_ITEM;
}

void Pirhana::ResetRect()
{
	/*_rectBond.left = _x;
	_rectBond.right = _rectBond.left + _sprite->texture->_Width;
	_rectBond.top = _y;
	_rectBond.bottom = _rectBond.top + _sprite->texture->_Height;*/

	_rectBond.left = _xTemp;
	_rectBond.right = _rectBond.left + _sprite->texture->_Width;
	_rectBond.top = _yTemp;
	_rectBond.bottom = _rectBond.top + _sprite->texture->_Height;
}

void Pirhana::Update(list<Object*>* staObjs, list<Object*>* dynObjs, DxInput* input, float gameTime)
{
	switch(object_State)
	{
	case IS_IDLE:
		timeStop += gameTime;
		if (timeStop > 1000)
		{
			timeStop = 0;
			ChangeState(IS_ACTIVING);
		}
		
		break;
	case IS_ACTIVING:
		_sprite->SetFrame(0,2);
		 
		
		//_y += _vy * gameTime ;
		_yTemp += _vy * gameTime ;

		/*if(_y< this->GetBond().bottom - 32-5)
			ChangeState(IS_MOVING);*/
		//if((_vy<0&&_y<_yOld-56)||(_vy>0&&_y>_yOld))				//Tới mức giới hạn
		//	ChangeState(IS_IDLE);

		//if((_vy<0 && _y<_yOld-(32 + 32/2 - 2)))				//Tới mức giới hạn
		//{
		//	_vy = -_vy;

		//}
		//if((_vy>0 && _y>_yOld))				//Tới mức giới hạn
		//{
		//	ChangeState(IS_IDLE);

		//}

		if((_vy<0 && _yTemp<_yOld-(32 + 32/2 - 2)))				//Tới mức giới hạn
		{
			_vy = -_vy;

		}
		if((_vy>0 && _yTemp>_yOld))				//Tới mức giới hạn
		{
			ChangeState(IS_IDLE);

		}

		ResetRect();
		break;
	 
	}
	_sprite->Update(gameTime);
	
	 
		

		 
}

void Pirhana::ChangeState(int state)
{
	object_State = state;
	switch(object_State)
	{
	case IS_IDLE:
		timeStop = 0;
		break;
	case IS_ACTIVING:
		/*if (_y < _yOld)
		{
			_vy = 1.5;
		} 
		else
		{ 
			_vy = -1.5;
		}*/
		_vy = -0.05;
		break;
	case IS_REMOVE:
		break;
	}
}